package db_main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	global2 "longmen/server/config/global"
	"longmen/server/internal/rpc/tencentim/imsdk"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
	"strconv"
)

func GetAnchorFansParam(param map[string]interface{}) (*models_main.AnchorFans, error) {
	fName := "GetAnchorFansParam"
	o := &models_main.AnchorFans{}
	table := global2.Live().Model(&models_main.AnchorFans{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetAnchorFanssParam(param map[string]interface{}, order string, page, size int) ([]*models_main.AnchorFans, error) {
	fName := "GetAnchorFanssParam"
	o := make([]*models_main.AnchorFans, 0)
	table := global2.Live().Model(&models_main.AnchorFans{})
	mysql.Condition(table, param, "", order, page, size)
	err := table.Find(&o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetAnchorFansForIds(param map[string]interface{}, column string) ([]int, error) {
	fName := "GetAnchorFansForIds"
	o := make([]int, 0)
	table := global2.Live().Model(&models_main.AnchorFans{})
	mysql.Condition(table, param, "", "create_time desc", 1, 100)
	err := table.Pluck(column, &o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetFansCount(param map[string]interface{}) (int64, error) {
	fName := "GetFansCount"
	var o int64
	table := global2.Live().Model(&models_main.AnchorFans{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Count(&o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

//转到live库
func InsertAnchorFans(fans *models_main.AnchorFans) error {
	fName := "InsertAnchorFans"
	err := global2.Live().Create(fans).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}

	imsdk.GetSdk().AddTags([]string{strconv.Itoa(fans.Fansid)}, []string{strconv.Itoa(fans.Anchorid)})

	if err != nil {
		global2.Log.Errorf("InsertAnchorFans %s %s", util.Convert(fName), err.Error())
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}

	return nil
}

/**
Modify July13 2022 Enzo
原来批量插入失败也不报错，改为挨个插入并计数
*/
func ClauseInsertAnchorsFans(fans []*models_main.AnchorFans) (successList []int, err error) {
	fName := "ClauseInsertAnchorsFans"
	successList = make([]int, 0)
	for _, v := range fans {
		err = InsertAnchorFans(v)
		if err == nil {
			successList = append(successList, v.Anchorid)
		}
	}
	if len(successList) == 0 {
		global2.Log.Errorf("6InsertAnchorFans9%s %s", util.Convert(fName), err.Error())
		return successList, err
	}
	/*err :=
		global.
			Live().
			Clauses(clause.OnConflict{
				Columns:   []clause.Column{{Name: "id"}},
				DoUpdates: clause.AssignmentColumns([]string{"anchorid", "fansid", "create_time", "live_id"}),
			}).
			Create(fans).
			Error
	if err != nil {
		return fmt.Errorf("%s%s", common.Convert(fName), err.Error())
	}*/
	return successList, nil
}

//取消关注：已转到live库
func DeleteAnchorFans(fans *models_main.AnchorFans) error {
	fName := "DeleteAnchorFans"
	err := global2.Live().Where("id = ?", fans.Id).Delete(fans).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}

	imsdk.GetSdk().DeleteTags([]string{strconv.Itoa(fans.Fansid)}, []string{strconv.Itoa(fans.Anchorid)})
	if err != nil {
		global2.Log.Errorf("DeleteAnchorFans %s %s", util.Convert(fName), err.Error())
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
